from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from data_dxm import data_dxm
from tools.sql import Sql


class TestDuxm:
    def driver(self,a):
        driver = a  # webdriver.Edge() / webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()
        return driver

    def dl1(self, drive, id, pw):
        time.sleep(1)
        drive.find_element(By.XPATH, "//*[@id='username']").send_keys(id)
        time.sleep(1)
        drive.find_element(By.XPATH, "//*[@id='password']").send_keys(pw)
        time.sleep(1)
        drive.find_element(By.XPATH, " // *[ @ id = 'login_btn']").click()
        time.sleep(2)
        a=drive.title
        drive.find_element(By.XPATH, "/html/body/div/div/header/nav/div/ul/li[1]/a/span").click()
        time.sleep(1)
        drive.find_element(By.XPATH, "/html/body/div/div/header/nav/div/ul/li[1]/ul/li[4]/a").click()
        time.sleep(1)
        return a
    def dl(self, drive, id, pw):
        time.sleep(1)
        drive.find_element(By.XPATH, "//*[@id='username']").send_keys(id)
        time.sleep(1)
        drive.find_element(By.XPATH, "//*[@id='password']").send_keys(pw)
        time.sleep(1)
        drive.find_element(By.XPATH, " // *[ @ id = 'login_btn']").click()
        time.sleep(1)

    def zc(self, driver, id, pw1, pw2):
        time.sleep(1)
        a = 0
        s1 = data_dxm.sql_da()
        for i in s1:
            a += 1
        driver.find_element(By.XPATH, " / html / body / div / div / div / div[2] / a").click()
        time.sleep(1)
        driver.find_element(By.XPATH, "//*[@id='username']").send_keys(id)
        time.sleep(1)
        driver.find_element(By.XPATH, "//*[@id='password']").send_keys(pw1)
        time.sleep(1)
        driver.find_element(By.XPATH, "//*[@id='repassword']").send_keys(pw2)
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="login_btn"]').click()
        time.sleep(1)
        b = 0
        sq = Sql('finance', 'user')
        s2 = sq.select()
        for i in s2:
            b += 1
        if b == a + 1:
            return '成功'
        else:
            driver.save_screenshot('erro2.jpg')
            return "失败"

    def card_name(self, driver, c_name):
        time.sleep(1)
        driver.find_element(By.XPATH, '// *[ @ id = "leftbaraside"] / div[2] / nav / ul / li[4] / a').click()  # 点击个人中心
        time.sleep(1)
        driver.find_element(By.XPATH,'// *[ @ id = "leftbaraside"] / div[2] / nav / ul / li[4] / ul / li[3] / a').click()
        time.sleep(1)
        driver.find_element(By.XPATH, '/ html / body / div / div / main / div / div / div[3] / div / div / div[1] / ul / li / button[1]').click()
        time.sleep(1)
        driver.find_element(By.XPATH, '// *[ @ id = "cardBank_update_input"]').clear()
        time.sleep(1)
        driver.find_element(By.XPATH, '// *[ @ id = "cardBank_update_input"]').send_keys(c_name)
        time.sleep(1)
        driver.find_element(By.XPATH, '// *[ @ id = "bankCard_update_btn"]').click()
        time.sleep(2)
        text1 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[3]/div/div/div[1]/div/h4').text
        # time.sleep(1)
        driver.find_element(By.XPATH, '// *[ @ id = "leftbaraside"] / div[2] / nav / ul / li[4] / a').click()  # 点击个人中心
        time.sleep(1)
        if text1 == c_name + " (借记卡)":
            return '成功'
        else:
            driver.save_screenshot('erro3.jpg')
            return "失败"

    def card_add(self, driver, bank_name, card):
        time.sleep(1)
        a = 0
        sq = Sql('finance', 'bankcard')
        s1 = sq.select()
        for i in s1:
            a += 1
        driver.find_element(By.XPATH, '// *[ @ id = "leftbaraside"] / div[2] / nav / ul / li[4] / a').click()  # 点击个人中心
        time.sleep(1)
        driver.find_element(By.XPATH, '// *[ @ id = "leftbaraside"] / div[2] / nav / ul / li[4] / ul / li[3] / a').click()
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="bankCard_add_modal_btn"]').click()
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="cardBank_add_input"]').send_keys(bank_name)
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="cardNum_add_input"]').send_keys(card)
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="bankCard_save_btn"]').click()
        time.sleep(1.5)
        driver.find_element(By.XPATH, '// *[ @ id = "leftbaraside"] / div[2] / nav / ul / li[4] / a').click()  # 点击个人中心
        b = 0
        s2 = sq.select()
        card = str(card)
        for i in s2:
            b += 1
        if b == a + 1 and len(card) == 16:
            return '成功'
        else:
            driver.save_screenshot('erro4.jpg')
            return "失败"

    def buy(self, driver):
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()  # 点击个人理财
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()  # 点击零钱理财
        time.sleep(1)
        driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button').click()  # 点击第一个投资
        time.sleep(1)
        driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys('666666')  # 输入密码
        time.sleep(1)
        driver.find_element(By.XPATH, '/html/body/div[3]/div[3]/a[1]').click()  # 点击确定
        time.sleep(1)
        text1 = driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text  # 获取弹窗的值
        time.sleep(1)
        driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[4]/button').click()  # 点击弹窗的确定
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()  # 点击个人理财
        return text1

    def loans(self, driver, money, days):
        time.sleep(1)
        driver.find_element(By.XPATH, ' //*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()  # 点击个人中心
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[2]/a').click()  # 点击我的借贷
        time.sleep(1)
        text1 = driver.find_element(By.CSS_SELECTOR, 'body > div > div > main > div > div > div > div > div.card-body > div.row > div:nth-child(2) > div > button:nth-child(7) > span').text
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/a').click()  # 点击金融工具
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a').click()
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="amount"]').send_keys(money)  # 输入借款金额
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="term"]').send_keys(days)  # 输入借款期限
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="submit"]').click()  # 点击申请
        time.sleep(1)
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()  # 点击确定
        time.sleep(2)
        driver.find_element(By.XPATH, ' //*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()  # 点击个人中心
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[2]/a').click()  # 点击我的借贷
        time.sleep(1)
        text2 = driver.find_element(By.CSS_SELECTOR,
                                    'body > div > div > main > div > div > div > div > div.card-body > div.row > div:nth-child(2) > div > button:nth-child(7) > span').text
        time.sleep(1)
        driver.find_element(By.XPATH, ' //*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()  # 点击个人中心
        if int(text2[:2:]) == int(text1[:2:]) + 1:
            return "成功"
        else:
            driver.save_screenshot('erro5.jpg')
            return "失败"
